/**
 * Created by Tang on 2017/7/14.
 */
var gulp = require('gulp'),
    webserver = require('gulp-webserver'),
    livereload = require('gulp-livereload'),
    sass = require('gulp-ruby-sass'),
    uglify = require('gulp-uglify'),
    rename = require('gulp-rename'),
    maps = require('gulp-sourcemaps'),
    imagemin = require('gulp-imagemin');


//自动刷新
gulp.task('server',function () {
    gulp.src('src')
        .pipe(webserver({
            livereload:true,
            open:true
        }))
});
//发布html事件;
gulp.task('html',function () {
    return gulp.src('src/**/*.html')
        .pipe(gulp.dest('dest'));
});
//发布sass事件;
gulp.task('sass',function () {
    return sass('src/sass/*.scss',{style:'compressed'})
        .on('error',function (err) {
            console.log(err)
        })
        .pipe(gulp.dest('dest/css'))
        .pipe(gulp.dest('src/css'))
});
//js文件压缩&重命名;
gulp.task('js',function () {
    return gulp.src('src/js/*.js')
        .pipe(maps.init())
        .pipe(uglify())
        .pipe(rename({suffix:'.min'}))
        .pipe(maps.write('maps'))
        .pipe(gulp.dest('dest/js'))
});
//图片压缩;
gulp.task('images',function () {
    gulp.src('src/img/*.*')
        .pipe(imagemin({
            progressive: true
        }))
        .pipe(gulp.dest('dest/img'))
});
//监听事件;
gulp.task('watch',function () {
    gulp.watch('src/**/*.html',['html']);
    gulp.watch('src/**/*.scss',['sass']);
    gulp.watch('src/**/*.js',['js']);
    gulp.watch('src/img/*.*',['images']);
});

//默认任务;
gulp.task('default',['server','html','watch','sass','js','images']);

